#include <iostream>
#include "CSocketProtocol.h"
#include <string.h>
#include "CSocketFactoryImpl.h"
#include "CSocketFactoryImpl2.h"
#include "CEncDesProtocol.h"

//抽象类在多继承中的应用
/*
class MainOp : public CSocketProtocol, public CEncDesProtocol
{

};
*/
class MainOp{
private:
        CSocketProtocol *sp;
        CEncDesProtocol *ed;
public:
        MainOp(){
                sp = NULL;
                ed = NULL;
        }

        MainOp(CSocketProtocol *sp, CEncDesProtocol *ed){
                this->sp = sp;
                this->ed = ed;
        }
        //
        void setCSocketProtocol(CSocketProtocol *sp){
                this->sp = sp;
        }
        void setCEncDesProtocol(CEncDesProtocol *ed){
                this->ed = ed;
        }
public:
        int SckSendAndRec(unsigned char *in, int inlen, unsigned char *out, int *outlen){
                int ret = 0;
                
                ret = sp->cltSocketInit();
                if(ret != 0){
                        goto End;
                }

                ret = sp->cltSocketSend(in, inlen);
                if(ret != 0){
                        goto End;
                }

                ret = sp->cltSocketRev(out,outlen);
                if(ret != 0){
                        goto End;
                }
        End:
                ret = sp->cltSocketDestory();
                return 0;
        }         
};

//面向抽象类编程，框架实现完毕
int SckSendAndRec(CSocketProtocol *sp, unsigned char *in, int inlen, unsigned char *out, int *outlen){
        int ret = 0;
        
        ret = sp->cltSocketInit();
        if(ret != 0){
                goto End;
        }

        ret = sp->cltSocketSend(in, inlen);
        if(ret != 0){
                goto End;
        }

        ret = sp->cltSocketRev(out,outlen);
        if(ret != 0){
                goto End;
        }
End:
        ret = sp->cltSocketDestory();
        return 0;
}

int main01(int argc, char const *argv[]){
        int ret = 0;
        unsigned char in[4096];
        int inlen;
        unsigned char out[4096];
        int outlen = 0;

        strcpy((char *)in, "aaaaddddaasdasda");
        inlen  =  9;

        CSocketProtocol *sp = NULL;
        //sp = new  CSocketFactoryImpl;
        sp = new CSocketFactoryImpl2;
        SckSendAndRec(sp, in, inlen, out, &outlen);
        if(ret != 0){
                printf("func SckSendAndRec() err:%d \n", ret);
                return ret;
        }


        delete sp;        
        return ret;
}
//用到了组合
//类中的属性通过 构造方法 setXXX 依赖注入
//IOC控制反转 你调用框架 框架反过来调用你的回调方法
//MVC 框架就是C 
//AOP
int main(int argc, char const *argv[])
{
        int ret = 0;
        unsigned char in[4096];
        int inlen;
        unsigned char out[4096];
        int outlen = 0;

        strcpy((char *)in, "aaaaddddaasdasda");
        inlen  =  9;

        CSocketFactoryImpl *sp = new  CSocketFactoryImpl;

        MainOp *$main = new MainOp;
        $main->setCSocketProtocol(sp);
        ret = $main->SckSendAndRec(in, inlen, out , &outlen);
        if(ret != 0){
                printf(" SckSendAndRec main err %d\n", ret);
                return ret;
        }

        delete sp;
        delete $main;
        return 0;
}
